﻿var salesportal = salesportal || {};
salesportal.employee = salesportal.employee || {};

// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function Employee(dataObj) {
    var self = this;

    this.Id = dataObj.Id;
    this.LegalName = dataObj.LegalName;
    this.DateOfBirth = ko.computed(function () {
        return moment(dataObj.DateOfBirth).format('YYYY-MM-DD');
    });
    this.Salary = ko.computed(function () {
        return numeral(dataObj.Salary).format('$0,0.00');
    });
    this.Status = ko.computed(function () {
        switch(dataObj.Status)
        {
            //{ text: "Unclassifed", value: "0" },
            //{ text: "Employed", value: "1" },
            //{ text: "Resigned", value: "2" }
            case 0:
                return "Unclassified";
            case 1:
                return "Employed";
            case 2:
                return "Resigned";
        }
        
    });

    
}

function EmployeeViewModel() {
    var self = this;

    // Non-editable catalog data - would come from the server
    // Data
    var self = this;
    self.employees = ko.observableArray([]);
    
    // Editable data
    // Computed data
    // Operations

    // Load initial state from server, convert it to Task instances, then populate self.tasks
    //$.getJSON("/tasks", function (allData) {
    //    var mappedTasks = $.map(allData, function (item) { return new Task(item) });
    //    self.tasks(mappedTasks);
    //});

    $.ajax({
        url: 'http://dev.sales.web/api/Employee',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            debugger;
            var mappedTasks = $.map(data, function (item) {
                return new Employee(item)
            });
            self.employees(mappedTasks);
        },
        error: function (x, y, z) {
            alert(x + '\n' + y + '\n' + z);
        }
    });
}

// Activates knockout.js
ko.applyBindings(new EmployeeViewModel());